//
//  ZLImageItem.m
//  CheXunWang
//
//  Created by zhu lin on 12/21/11.
//  Copyright 2011 Zhulin. All rights reserved.
//

#import "ZLImageItem.h"

@interface ZLImageItem()
@property (nonatomic, retain) UIImageView *backImageView;
@property (nonatomic, retain) UIImageView *labelBackImageView;
@property (nonatomic, retain) UILabel *imageIndexLabel;
@property (nonatomic, retain) UIButton *coverButton;

@property (nonatomic, assign) id delegate;
@end


@implementation ZLImageItem
@synthesize backImageView;
@synthesize imageItemView;
@synthesize labelBackImageView;
@synthesize imageIndexLabel;
@synthesize coverButton;
@synthesize index, delegate;

- (id)initWithFrame:(CGRect)frame andDelegate:(id)del {
    
    self = [super initWithFrame:frame];
    if (self) {
		self.delegate = del;
		
        // backImage
		self.backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height)];
		self.backImageView.image = [UIImage imageNamed:@"ios_car_brand_filter_pic_frame_normal.png"];
		[self addSubview:self.backImageView];
		
		// imageItem
		self.imageItemView = [[UIImageView alloc] initWithFrame:CGRectMake(4.0f, 4.0f, frame.size.width - 8.0f, frame.size.height - 8.0f)];
		[self addSubview:self.imageItemView];
		
		// index label
		self.labelBackImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"page_frame.png"]];
		labelBackImageView.alpha = 0.7; 
		[self addSubview:labelBackImageView];
		[labelBackImageView release];
		
		self.imageIndexLabel = [[UILabel alloc] init];
		self.imageIndexLabel.backgroundColor = [UIColor clearColor];
		self.imageIndexLabel.textColor = [UIColor whiteColor];
		self.imageIndexLabel.font = [UIFont systemFontOfSize:10.0];
		self.imageIndexLabel.textAlignment = UITextAlignmentCenter;
		[self addSubview:self.imageIndexLabel];
		
		// coverbutton
		self.coverButton = [UIButton buttonWithType:UIButtonTypeCustom];
		[self.coverButton setFrame:CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height)];
		[self.coverButton setBackgroundColor:[UIColor clearColor]];
		[self.coverButton addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown];
		[self.coverButton addTarget:self action:@selector(touchCancel:) forControlEvents:UIControlEventTouchCancel];
		[self.coverButton addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside];
		[self addSubview:self.coverButton];
    }
    return self;
}

- (void)dealloc {
    [super dealloc];
	[backImageView release];
}

- (void)setIndex:(int)_index
{
	index = _index;
	
	NSString *indexStr = [NSString stringWithFormat:@"%d", _index];
	CGSize indexTagSize = [indexStr sizeWithFont:[UIFont systemFontOfSize:14.0]];
	CGRect indexLabelRect = CGRectMake(self.bounds.size.width - 3.0f - indexTagSize.width - 10.0, self.bounds.size.height - 3.0f - 15.0f, indexTagSize.width + 10.0, 15.0f);
	
	self.labelBackImageView.frame = indexLabelRect;
	self.imageIndexLabel.frame = indexLabelRect;
	self.imageIndexLabel.text = indexStr;
}

#pragma mark -
#pragma mark actions
- (void)touchDown:(id)sender
{
	self.backImageView.image = [UIImage imageNamed:@"ios_car_brand_filter_pic_frame_pressed.png"];
}

- (void)touchCancel:(id)sender
{
	self.backImageView.image = [UIImage imageNamed:@"ios_car_brand_filter_pic_frame_normal.png"];
}

- (void)touchUpInside:(id)sender
{
	if ([self.delegate respondsToSelector:@selector(showLargePicture:)]) {
		[self.delegate showLargePicture:self.index];
	}
	
	self.backImageView.image = [UIImage imageNamed:@"ios_car_brand_filter_pic_frame_normal.png"];
}

@end
